/*
 * PanelEnemy.java
 *
 * Created on January 2, 2008, 8:08 PM
 *
 * Copyright (C) 2007-2008  Hylke van der Schaaf
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package hextd.UI;

import hextd.creep.Creep;
import hextd.core.ContextNormal;
import hextd.wave.Path;
import hextd.wave.PathEmpty;
import hextd.wave.Wave;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.Iterator;

/**
 *
 * @author  hylke
 */
public class PanelEnemy extends javax.swing.JPanel {

	private ArrayList<Creep> enemies = new ArrayList<Creep>();
	private ContextNormal contextLocal, contextFull;
	private int pHeight = 0;
	private int pWidth = 0;
	private float scale = 0;
	private int gameTime = 0;

	/** Creates new form PanelEnemy */
	public PanelEnemy() {
		this.contextLocal = new ContextNormal(null);
		this.contextLocal.setScaledCanvas(new ScaledCanvas() {

			float paintScale, strokeScale, offsetX, offsetY;

			@Override
			public float getPaintScale() {
				return paintScale;
			}

			@Override
			public float getStrokeScale() {
				return strokeScale;
			}

			@Override
			public void setPaintScale(float scale) {
				paintScale = scale;
			}

			@Override
			public void setStrokeScale(float scale) {
				strokeScale = scale;
			}

			@Override
			public float getOffsetX() {
				return offsetX;
			}

			@Override
			public void setOffsetX(float offsetX) {
				this.offsetX = offsetX;
			}

			@Override
			public float getOffsetY() {
				return offsetY;
			}

			@Override
			public void setOffsetY(float offsetY) {
				this.offsetY = offsetY;
			}

			@Override
			public float getHexPaintWidth() {
				throw new UnsupportedOperationException("Not supported yet.");
			}

			@Override
			public void setHexPaintWidth(float hexPaintWidth) {
				throw new UnsupportedOperationException("Not supported yet.");
			}
		});
	}

	public void setContext(ContextNormal c) {
		this.contextFull = c;

		initComponents();

		this.contextLocal.setContextUrl(c.getContextUrl());
		this.contextLocal.colors = c.colors;
	}

	public synchronized void clearEnemies() {
		this.enemies.clear();
		this.contextLocal.getPaths().clear();
	}

	public synchronized void addEnemy(Wave.EnemyInstance e) {
		ArrayList<Path> paths = this.contextLocal.getPaths();
		PathEmpty p = new PathEmpty();
		paths.add(p);
		this.enemies.add(e.type.getCopy(this.contextLocal, -1, paths.size() - 1, 0, e.health, e.price, e.score, e.size));
		recalculateSize();
	}

//	@Override
//	public Dimension getPreferredSize() {
//		return new Dimension(195, 40);
//	}
	public synchronized void recalculateSize() {
		this.pWidth = this.getWidth();
		this.contextLocal.getCache().clearCache();
		if (this.enemies.size() > 0) {
			this.pHeight = Math.min(this.pWidth / this.enemies.size(), this.getHeight());
			this.scale = this.pHeight;

			this.contextLocal.getScaledCanvas().setPaintScale(this.scale);
			this.contextLocal.getScaledCanvas().setStrokeScale(scale / 40f);
			int nr = 0;
			float x = 0;
			float y = 0.5f;
			for (Iterator<Path> i = this.contextLocal.getPaths().iterator(); i.hasNext();) {
				PathEmpty p = (PathEmpty) i.next();
				x = 0.5f + nr;
				p.setPoint(x, y);
				nr++;
			}
			for (Iterator<Creep> i = this.enemies.iterator(); i.hasNext();) {
				Creep e = i.next();
				e.resetScale();
			}
		}
	}

	public void doTick(int gameTime) {
		this.gameTime = gameTime;
		try {
			for (Iterator<Creep> i = enemies.iterator(); i.hasNext();) {
				Creep e = i.next();
				e.doTick(gameTime);
			}
		} catch (ConcurrentModificationException e) {
		}
		this.repaint();
	}

	@Override
	public void paint(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
		if (this.enemies.size() > 0) {
			g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
			g2.setColor(Color.BLACK);
			g2.fillRect(0, 0, this.pWidth, this.pHeight);
			try {
				for (Iterator<Creep> i = this.enemies.iterator(); i.hasNext();) {
					Creep e = i.next();
					e.paint(g2, this.gameTime);
				}
			} catch (ConcurrentModificationException e) {
			}
		} else {
			g2.setColor(Color.BLACK);
			g2.fillRect(0, 0, this.getWidth(), this.getHeight());
		}
	}

	private void mouseOver(int x) {
		int nr = (int) (x / this.scale);
		if (nr < this.enemies.size()) {
			Creep e = this.enemies.get(nr);
			this.contextFull.setInfoText(e.getInfoString());
		}
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setBackground(this.contextFull.colors.getColorBackGround());
        setBorder(null);
        setForeground(this.contextFull.colors.getColorForeGround());
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                formComponentResized(evt);
            }
        });
        addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                formMouseMoved(evt);
            }
        });
        setLayout(null);
    }// </editor-fold>//GEN-END:initComponents
    private void formMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseMoved
		this.mouseOver(evt.getX());
    }//GEN-LAST:event_formMouseMoved

    private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized
		this.recalculateSize();
    }//GEN-LAST:event_formComponentResized
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
